package com.lianyu507.util;

import com.lianyu507.pojo.MbManager;
import com.lianyu507.pojo.MbUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;


/**
 * 用户的上下文
 */
// 通过监听请求的创建 将请求扔到threadlocal里 源代码RequestContextListener类
public class UserContext{

    public static final String LOGININFO_IN_SESSION_MANAGER = "LOGININFO_IN_SESSION_MANAGER";

    //注意web.xml配置该监听器 RequestContextHolder
    private static ServletContext getSession() {
        return ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest().getSession().getServletContext();
    }

    public static void putCurrentUser(MbUser user) {
        getSession().setAttribute(user.getToken(), user);
    }

    public static MbUser getCurrentUser() {
        return  (MbUser) getSession().getAttribute(ThreadLocalUtil.getLoginToken());
    }

    public static void putCurrentManager(MbManager manager) {
        getSession().setAttribute(LOGININFO_IN_SESSION_MANAGER, manager);
    }

    public static MbManager getCurrentManager() {
        return  (MbManager) getSession().getAttribute(LOGININFO_IN_SESSION_MANAGER);
    }

}







